package com.ruoyi.tuanzhang.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.listener.PageReadListener;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.tuanzhang.domain.DarenBaseDo;
import com.ruoyi.tuanzhang.domain.SelfAddDarenLogDo;
import com.ruoyi.tuanzhang.domain.excelDto.SelfAddDarenExcelDto;
import com.ruoyi.tuanzhang.mapper.DarenBaseMapper;
import com.ruoyi.tuanzhang.mapper.SelfAddDarenLogMapper;
import com.ruoyi.tuanzhang.service.IDarenBaseService;
import com.ruoyi.tuanzhang.service.ISelfAddDarenLogService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 添加达人记录
 *
 */
@Service
public class SelfAddDarenLogServiceImpl
        extends ServiceImpl<SelfAddDarenLogMapper, SelfAddDarenLogDo>
        implements ISelfAddDarenLogService{

    private static final Logger logger = LoggerFactory.getLogger(SelfAddDarenLogServiceImpl.class);

    @Resource
    private IDarenBaseService darenBaseService;


    @Override
    public String importAddDarenLog(String filePath) {
        EasyExcel.read(filePath, SelfAddDarenExcelDto.class,
                new PageReadListener<SelfAddDarenExcelDto>(dataList -> {
            // 添加列表
            List<SelfAddDarenLogDo> selfAddDarenLogDos = JSONArray.parseArray(JSONArray.toJSONString(dataList), SelfAddDarenLogDo.class);
            this.baseMapper.insert(selfAddDarenLogDos);
        })).sheet().doRead();
        return "添加完成";
    }

    @Override
    public String exportAddDarenList(String filePath) {
        QueryWrapper<DarenBaseDo> wrapper = new QueryWrapper<>();
        wrapper.isNull("owner")
                .and(i -> i.isNotNull("wechat")
                        .or().isNotNull("phone"));
        List<DarenBaseDo> list = darenBaseService.list(wrapper);
        for (DarenBaseDo darenBaseDo : list) {
//            logger.info(JSON.toJSONString(darenBaseDo));
        }

        return "";
    }

}
